package com.shop.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.entity.pojo.*;
import com.shop.user.mapper.CostMapper;
import com.shop.user.mapper.ImagesMapper;
import com.shop.user.mapper.StyleMapper;
import com.shop.user.service.CostService;
import com.shop.utils.common.enums.Code;
import com.shop.utils.common.enums.Message;
import com.shop.utils.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service
public class CostServiceImpl extends ServiceImpl<CostMapper, Cost> implements CostService {

    @Autowired
    private CostMapper costMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ImagesMapper imagesMapper;
    @Autowired
    private StyleMapper styleMapper;

    @Override
    public Result<List<Cost>> getStyleCost(Long styleId) {
        if (styleId==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        String k=Message.WeChat_Key_Style +styleId;
        Map costList = redisTemplate.opsForHash().entries(k);
        Long check = redisTemplate.opsForHash().size(k);
        if (check==null) {
            return Result.okResult(costList);
        }
        List<Cost> list = costMapper.selectList(Wrappers.<Cost>lambdaQuery()
                .eq(Cost::getStyleId, styleId)
                .eq(Cost::getState,Message.Put));
        Style style = styleMapper.selectById(styleId);
        Map<String,Object> v=new HashMap<>();
        v.put("list",list);
        v.put("images",style.getStyleImages());
        redisTemplate.opsForHash().putAll(k,v);
        redisTemplate.expire(k,1,TimeUnit.HOURS);
        return Result.okResult(v);
    }

}
